package com.lxp.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.eduService.domain.EduCourse;
import com.lxp.eduService.domain.vo.CourseInfo;
import com.lxp.eduService.domain.vo.publish.CoursePublishVo;
import com.lxp.eduService.domain.vo.queryVo.CourseQuery;
import com.lxp.eduService.service.EduCourseService;
import com.lxp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lxp
 * @since 2021-11-20
 */
@RestController
@RequestMapping("/eduService/edu-course")

public class EduCourseController {
    @Autowired
    private EduCourseService service;

    //添加课程
    @PostMapping("/addCourse")
    public R addCourse(@RequestBody CourseInfo courseInfo){
        String courseId=service.addCourse(courseInfo);
        return R.ok().data("courseId",courseId);
    }

    //根据课程id 查询出courseInfo
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){

        CourseInfo courseInfo=service.getCourseInfo(courseId);
        return R.ok().data("courseInfo",courseInfo);
    }

    //修改课程
    @PostMapping("/updateCourse")
    public R updateCourse(@RequestBody CourseInfo courseInfo){

        service.updateCourse(courseInfo);
        return R.ok();
    }

    //最终发布课程信息显示
    @GetMapping("/publishCourseById/{courseId}")
    public R publishCourseById(@PathVariable String courseId){

        CoursePublishVo coursePublishVo = service.publishCourse(courseId);
        return R.ok().data("coursePublish",coursePublishVo);
    }

    //最终发布课程
    @PostMapping("/updateCourseStatus/{courseId}")
    public R updateCourseStatus(@PathVariable String courseId){
        //修改课程发布状态码 为Normal 即为发布
        EduCourse eduCourse =new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        service.updateById(eduCourse);
        return R.ok();
    }


    //课程列表分页带条件查询显示
    @PostMapping("/getAllCourse/{current}/{size}")
    public R getAllCourse(@PathVariable long current,
                          @PathVariable int size,
                          @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> page =new Page<>(current,size);

        //封装条件
        String title =courseQuery.getTitle();
        String status=courseQuery.getStatus();
        String beginTime =courseQuery.getBeginTime();
        String endTime =courseQuery.getEndTime();

        QueryWrapper<EduCourse> wrapper =new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if(!StringUtils.isEmpty(beginTime)){
            wrapper.ge("gmt_create",beginTime);
        }
        if(!StringUtils.isEmpty(endTime)){
            wrapper.le("gmt_create",endTime);
        }

        service.page(page,wrapper);
        List<EduCourse> list = page.getRecords();
        long total = page.getTotal();

        return R.ok().data("list",list).data("total",total);
    }


    //根据课程id 删除课程
    @DeleteMapping("{courseId}")
    public R deleteByCourseId(@PathVariable String courseId){
        service.deleteByCourseId(courseId);

        return R.ok();
    }
}

